{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## フォントの色や設定を変更する\n",
    "\n",
    "* フォントの指定\n",
    "    * serFont()\n",
    "        * 引数は(フォント名、書体、ポイント)\n",
    "            * フォント名\n",
    "                * Dialog\n",
    "                * DialogInput\n",
    "                * Monospaced\n",
    "                * Serif\n",
    "                * SansSerif\n",
    "                * Symbol\n",
    "            * 書体\n",
    "                * Font.PLAIN（標準）\n",
    "                * Font.BOLD（太字）\n",
    "                * Font.ITALIC（斜体）\n",
    "            * 使えるフォント\n",
    "                * Windowsの場合はMSゴシック、MS明朝も使える\n",
    "                * フォントがない場合はデフォルトのフォントで表示される\n",
    "* フォントの色の指定\n",
    "    * Color定数\n",
    "        * Color.blueなど\n",
    "    * Colorコンストラクタ\n",
    "        * new Color(64,64,64)など\n",
    "* ボタン\n",
    "    * 背景色\n",
    "        * setBackground()メソッド\n",
    "    * 前景色(文字色)\n",
    "        * setForeground()メソッド"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%bash\n",
    "### 変数の設定\n",
    "name=GUITest6\n",
    "#classpath=\".:/root/git_jupyter_notebook/Java/postgresql-42.1.1.jar\"\n",
    "#classpath=\".:/Users/ftakao2007/jupyter/jupyter_notebook/Java/postgresql-42.1.1.jar\"\n",
    "\n",
    "### ソースの編集\n",
    "cat <<- EOS > ${name}.java\n",
    "\n",
    "import java.awt.*;\n",
    "import java.awt.event.*;\n",
    "\n",
    "class PrefFrame extends Frame implements ActionListener {\n",
    "    Label lb1;\n",
    "    TextArea txtar1;\n",
    "    Button btn1, btn2, btn3;\n",
    "    \n",
    "    public PrefFrame(String title) {\n",
    "        setTitle(title);\n",
    "\n",
    "        addWindowListener(new WindowAdapter() {\n",
    "            public void windowClosing(WindowEvent e) {\n",
    "                System.exit(0);\n",
    "            }\n",
    "        });\n",
    "\n",
    "        //# ラベル\n",
    "        lb1 = new Label(\"ボタンをクリックしてください。\",Label.CENTER);\n",
    "        //# フォントがSerif、書体がBOLD、ポイントが14になる\n",
    "        Font f = new Font(\"Serif\", Font.BOLD, 14);\n",
    "        lb1.setFont(f);\n",
    "        //# 文字の色が青色になる\n",
    "        lb1.setForeground(Color.blue);\n",
    "        add(lb1, BorderLayout.NORTH);\n",
    "        \n",
    "        //# テキストエリア\n",
    "        txtar1 = new TextArea();\n",
    "        //# フォントがDialog、書体がPLAIN(標準)、ポイントが16になる\n",
    "        txtar1.setFont(new Font(\"Dialog\", Font.PLAIN, 16));\n",
    "        //# 文字の色がダークグレイになる\n",
    "        txtar1.setForeground(new Color(64,64,64));\n",
    "        add(txtar1, BorderLayout.CENTER);\n",
    "        \n",
    "        //# パネル\n",
    "        Panel pn1 = new Panel();\n",
    "        pn1.setLayout(new GridLayout(1, 3));\n",
    "        \n",
    "        //# ボタン1\n",
    "        btn1 = new Button(\"表示\");\n",
    "        btn1.addActionListener(this);\n",
    "        pn1.add(btn1);\n",
    "        \n",
    "        //# ボタン2\n",
    "        btn2 = new Button(\"クリア\");\n",
    "        btn2.addActionListener(this);\n",
    "        pn1.add(btn2);\n",
    "\n",
    "        //# ボタン3\n",
    "        btn3 = new Button(\"終了\");\n",
    "        btn3.addActionListener(this);\n",
    "        //# 背景色がダークグレイになる\n",
    "        btn3.setBackground(new Color(255,0,255));\n",
    "        //# 前景色(文字色)がマゼンダになる\n",
    "        btn3.setForeground(new Color(255,255,255));\n",
    "        pn1.add(btn3);\n",
    "        \n",
    "        //# パネル\n",
    "        add(pn1, BorderLayout.SOUTH);\n",
    "    }\n",
    "  \n",
    "    public void actionPerformed (ActionEvent e) {\n",
    "        if (e.getSource() == btn1) {\n",
    "            prefDisplay();\n",
    "        } else if (e.getSource() == btn2) {\n",
    "            txtar1.setText(\"\");\n",
    "        } else {\n",
    "            System.exit(0);\n",
    "        }\n",
    "    }\n",
    "    \n",
    "    //# クラス内からしか使わないのでアクセス修飾子はprivate\n",
    "    private void prefDisplay () {\n",
    "        StringBuffer temp = new StringBuffer();\n",
    "        temp.append(\"1\" + \"\\t\");\n",
    "        temp.append(\"Hokkaido\" + \"\\n\");\n",
    "        temp.append(\"2\" + \"\\t\");\n",
    "        temp.append(\"Aomori\" + \"\\n\");\n",
    "        temp.append(\"3\" + \"\\t\");\n",
    "        temp.append(\"Iwate\" + \"\\n\");\n",
    "        temp.append(\"4\" + \"\\t\");\n",
    "        temp.append(\"Miyagi\" + \"\\n\");\n",
    "        temp.append(\"5\" + \"\\t\");\n",
    "        temp.append(\"Akita\" + \"\\n\");\n",
    "        txtar1.setText(temp.toString());\n",
    "    }\n",
    "            \n",
    "}\n",
    "\n",
    "public class $name {\n",
    "    public static void main(String args[]){\n",
    "        PrefFrame frm = new PrefFrame(\"都道府県マスター\");\n",
    "        //# フレームの設定\n",
    "        frm.setLocation(300,200);\n",
    "        frm.setSize(250,350);\n",
    "        frm.setBackground(Color.LIGHT_GRAY);\n",
    "        frm.setVisible(true);\n",
    "    }\n",
    "}\n",
    "EOS\n",
    "\n",
    "### コンパイル\n",
    "#javac ${name}.java\n",
    "javac -encoding UTF-8 ${name}.java\n",
    "\n",
    "### 実行\n",
    "#java -classpath ${classpath} ${name}\n",
    "java ${name}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ダイアログボックスの表示\n",
    "\n",
    "* ダイアログボックスの作成\n",
    "    * Dialogクラスを継承したクラスを作る\n",
    "        * 使い方はほとんどFrameクラスと同じ\n",
    "        * ダイアログボックスは単独では存在できない\n",
    "            * 親コンポーネントが必要"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%bash\n",
    "### 変数の設定\n",
    "name=GUITest6\n",
    "#classpath=\".:/root/git_jupyter_notebook/Java/postgresql-42.1.1.jar\"\n",
    "#classpath=\".:/Users/ftakao2007/jupyter/jupyter_notebook/Java/postgresql-42.1.1.jar\"\n",
    "\n",
    "### ソースの編集\n",
    "cat <<- EOS > ${name}.java\n",
    "\n",
    "import java.awt.*;\n",
    "import java.awt.event.*;\n",
    "\n",
    "class PrefFrame extends Frame implements ActionListener {\n",
    "    Label lb1;\n",
    "    TextArea txtar1;\n",
    "    Button btn1, btn2, btn3;\n",
    "    //# ダイアログボックスのクラス変数を宣言\n",
    "    ExitDialog dlog;\n",
    "    \n",
    "    public PrefFrame(String title) {\n",
    "        setTitle(title);\n",
    "\n",
    "        addWindowListener(new WindowAdapter() {\n",
    "            public void windowClosing(WindowEvent e) {\n",
    "                System.exit(0);\n",
    "            }\n",
    "        });\n",
    "\n",
    "        //# ラベル\n",
    "        lb1 = new Label(\"ボタンをクリックしてください。\",Label.CENTER);\n",
    "        Font f = new Font(\"Serif\", Font.BOLD, 14);\n",
    "        lb1.setFont(f);\n",
    "        lb1.setForeground(Color.blue);\n",
    "        add(lb1, BorderLayout.NORTH);\n",
    "        \n",
    "        //# テキストエリア\n",
    "        txtar1 = new TextArea();\n",
    "        txtar1.setFont(new Font(\"Dialog\", Font.PLAIN, 16));\n",
    "        txtar1.setForeground(new Color(64,64,64));\n",
    "        add(txtar1, BorderLayout.CENTER);\n",
    "        \n",
    "        //# パネル\n",
    "        Panel pn1 = new Panel();\n",
    "        pn1.setLayout(new GridLayout(1, 3));\n",
    "        \n",
    "        //# ボタン設定。今後配列として利用することを想定\n",
    "        btn1 = new Button(\"表示\");\n",
    "        btn2 = new Button(\"クリア\");\n",
    "        btn3 = new Button(\"終了\");\n",
    "        \n",
    "        btn3.setForeground(new Color(255, 0, 0));\n",
    "\n",
    "        btn1.addActionListener(this);\n",
    "        btn2.addActionListener(this);\n",
    "        btn3.addActionListener(this);\n",
    "        \n",
    "        pn1.add(btn1);\n",
    "        pn1.add(btn2);\n",
    "        pn1.add(btn3);\n",
    "        \n",
    "        //# パネル\n",
    "        add(pn1, BorderLayout.SOUTH);\n",
    "        \n",
    "        //# ダイアログボックス生成\n",
    "        //# 一番目の引数thisは親コンポーネント(Window自身)\n",
    "        //# 二番目の引数はタイトル\n",
    "        dlog = new ExitDialog(this, \"プログラムを終了しますか？\");\n",
    "    }\n",
    "  \n",
    "    public void actionPerformed (ActionEvent e) {\n",
    "        if (e.getSource() == btn1) {\n",
    "            prefDisplay();\n",
    "            //# ラベルの文字を変更している\n",
    "            lb1.setText(\"表示しました\");\n",
    "        } else if (e.getSource() == btn2) {\n",
    "            txtar1.setText(\"\");\n",
    "            lb1.setText(\"クリアしました\");\n",
    "        } else {\n",
    "            //# ダイアログボックスを表示する命令に変更\n",
    "            //# 参照型変数dlogを使ってダイアログボックスのメソッドにアクセスする\n",
    "            dlog.setVisible(true);\n",
    "        }\n",
    "    }\n",
    "    \n",
    "    //# クラス内からしか使わないのでアクセス修飾子はprivate\n",
    "    private void prefDisplay () {\n",
    "        StringBuffer temp = new StringBuffer();\n",
    "        temp.append(\"1\" + \"\\t\");\n",
    "        temp.append(\"Hokkaido\" + \"\\n\");\n",
    "        temp.append(\"2\" + \"\\t\");\n",
    "        temp.append(\"Aomori\" + \"\\n\");\n",
    "        temp.append(\"3\" + \"\\t\");\n",
    "        temp.append(\"Iwate\" + \"\\n\");\n",
    "        temp.append(\"4\" + \"\\t\");\n",
    "        temp.append(\"Miyagi\" + \"\\n\");\n",
    "        temp.append(\"5\" + \"\\t\");\n",
    "        temp.append(\"Akita\" + \"\\n\");\n",
    "        txtar1.setText(temp.toString());\n",
    "    }\n",
    "            \n",
    "}\n",
    "\n",
    "//# ActinListernerを実装したオリジナルダイアログボックスクラスを定義\n",
    "class ExitDialog extends Dialog implements ActionListener {\n",
    "    public ExitDialog(Frame parent, String title){\n",
    "        //# スーパークラスのコンストラクタを呼び出す\n",
    "        super(parent, title);\n",
    "        \n",
    "        //# ダイアログボックスを閉じるとき\n",
    "        addWindowListener( new WindowAdapter(){\n",
    "            public void windowClosing(WindowEvent e) {\n",
    "                setVisible(false);\n",
    "            }\n",
    "        });\n",
    "        \n",
    "        //# ダイアログボックス設定\n",
    "        setLocation(320, 300);\n",
    "        setSize(220,80);\n",
    "        setLayout(new FlowLayout(FlowLayout.CENTER,10,10));\n",
    "        setBackground(Color.LIGHT_GRAY);\n",
    "        \n",
    "        //ボタンの設定\n",
    "        Button btnYes = new Button(\"はい\");\n",
    "        Button btnNo = new Button(\"いいえ\");\n",
    "        \n",
    "        Font f = new Font (\"Dialog\", Font.PLAIN, 14);\n",
    "        btnYes.setFont(f);\n",
    "        btnNo.setFont(f);\n",
    "        btnYes.setForeground(new Color(255,0,0));\n",
    "        \n",
    "        btnYes.addActionListener(this);\n",
    "        btnNo.addActionListener(this);\n",
    "        \n",
    "        add(btnYes);\n",
    "        add(btnNo);\n",
    "    }\n",
    "    \n",
    "    //# イベントハンドラactionPerfomed()メソッド\n",
    "    public void actionPerformed(ActionEvent e){\n",
    "        //# getActionCommandメソッドでクリックされたボタンのコマンド名を取得し、文字列として変数に格納している\n",
    "        //# ここはコマンド名が無いのでボタンに表示されている文字が取得される\n",
    "        String str = e.getActionCommand();\n",
    "        //# 文字列は参照型なのでequals()メソッドを使う。==は使えない\n",
    "        if( str.equals(\"はい\")){\n",
    "            System.exit(0);        \n",
    "        } else if( str.equals(\"いいえ\")){\n",
    "            setVisible(false);\n",
    "        }\n",
    "    }\n",
    "}\n",
    "\n",
    "public class $name {\n",
    "    public static void main(String args[]){\n",
    "        PrefFrame frm = new PrefFrame(\"都道府県マスター\");\n",
    "        //# フレームの設定\n",
    "        frm.setLocation(300,200);\n",
    "        frm.setSize(250,350);\n",
    "        frm.setBackground(Color.LIGHT_GRAY);\n",
    "        frm.setVisible(true);\n",
    "    }\n",
    "}\n",
    "EOS\n",
    "\n",
    "### コンパイル\n",
    "#javac ${name}.java\n",
    "javac -encoding UTF-8 ${name}.java\n",
    "\n",
    "### 実行\n",
    "#java -classpath ${classpath} ${name}\n",
    "java ${name}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## メニューバーを表示する\n",
    "\n",
    "* "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%bash\n",
    "### 変数の設定\n",
    "name=GUITest7\n",
    "#classpath=\".:/root/git_jupyter_notebook/Java/postgresql-42.1.1.jar\"\n",
    "#classpath=\".:/Users/ftakao2007/jupyter/jupyter_notebook/Java/postgresql-42.1.1.jar\"\n",
    "\n",
    "### ソースの編集\n",
    "cat <<- EOS > ${name}.java\n",
    "\n",
    "import java.awt.*;\n",
    "import java.awt.event.*;\n",
    "\n",
    "class PrefFrame extends Frame implements ActionListener {\n",
    "    Label lb1;\n",
    "    TextArea txtar1;\n",
    "    Button btn1, btn2, btn3;\n",
    "    ExitDialog dlog;\n",
    "    MenuItem mi1, mi2, mi3, mi4, mi5, mi6, mi7;\n",
    "    \n",
    "    public PrefFrame(String title) {\n",
    "        setTitle(title);\n",
    "\n",
    "        addWindowListener(new WindowAdapter() {\n",
    "            public void windowClosing(WindowEvent e) {\n",
    "                System.exit(0);\n",
    "            }\n",
    "        });\n",
    "        \n",
    "        //# メニューバーの生成\n",
    "        MenuBar mb = new MenuBar();\n",
    "        \n",
    "        //# メニューの生成\n",
    "        Menu mn1 = new Menu(\"ファイル\");\n",
    "        Menu mn2 = new Menu(\"編集\");\n",
    "        Menu mn3 = new Menu(\"検索\");\n",
    "        \n",
    "        //# メニュー項目の生成\n",
    "        mi1 = new MenuItem(\"表示\");\n",
    "        mi2 = new MenuItem(\"クリア\");\n",
    "        mi3 = new MenuItem(\"終了\");\n",
    "        \n",
    "        //# イベントリスナーの登録\n",
    "        mi1.addActionListener(this);\n",
    "        mi2.addActionListener(this);\n",
    "        mi3.addActionListener(this);\n",
    "        \n",
    "        //# メニューへの追加\n",
    "        mn1.add(mi1);\n",
    "        mn1.add(mi2);\n",
    "        mn1.addSeparator();\n",
    "        mn1.add(mi3);\n",
    "        \n",
    "        //# メニューバーへの追加\n",
    "        mb.add(mn1);\n",
    "        mb.add(mn2);\n",
    "        mb.add(mn3);\n",
    "        \n",
    "        //# メニューバーをフレームに追加\n",
    "        setMenuBar(mb);\n",
    "\n",
    "        //# ラベル\n",
    "        lb1 = new Label(\"ボタンをクリックしてください。\",Label.CENTER);\n",
    "        Font f = new Font(\"Serif\", Font.BOLD, 14);\n",
    "        lb1.setFont(f);\n",
    "        lb1.setForeground(Color.blue);\n",
    "        add(lb1, BorderLayout.NORTH);\n",
    "        \n",
    "        //# テキストエリア\n",
    "        txtar1 = new TextArea();\n",
    "        txtar1.setFont(new Font(\"Dialog\", Font.PLAIN, 16));\n",
    "        txtar1.setForeground(new Color(64,64,64));\n",
    "        add(txtar1, BorderLayout.CENTER);\n",
    "        \n",
    "        //# パネル\n",
    "        Panel pn1 = new Panel();\n",
    "        pn1.setLayout(new GridLayout(1, 3));\n",
    "        \n",
    "        //# ボタン設定。今後配列として利用することを想定\n",
    "        btn1 = new Button(\"表示\");\n",
    "        btn2 = new Button(\"クリア\");\n",
    "        btn3 = new Button(\"終了\");\n",
    "        \n",
    "        btn3.setForeground(new Color(255, 0, 0));\n",
    "\n",
    "        btn1.addActionListener(this);\n",
    "        btn2.addActionListener(this);\n",
    "        btn3.addActionListener(this);\n",
    "        \n",
    "        pn1.add(btn1);\n",
    "        pn1.add(btn2);\n",
    "        pn1.add(btn3);\n",
    "        \n",
    "        //# パネル\n",
    "        add(pn1, BorderLayout.SOUTH);\n",
    "        \n",
    "        //# ダイアログボックス生成\n",
    "        dlog = new ExitDialog(this, \"プログラムを終了しますか？\");\n",
    "    }\n",
    "  \n",
    "    public void actionPerformed (ActionEvent e) {\n",
    "        Object obj = e.getSource();\n",
    "        if (obj == btn1 || obj == mi1) {\n",
    "            prefDisplay();\n",
    "            lb1.setText(\"表示しました\");\n",
    "        } else if (obj == btn2 || obj == mi2) {\n",
    "            txtar1.setText(\"\");\n",
    "            lb1.setText(\"クリアしました\");\n",
    "        } else if (obj == btn3 || obj == mi3){\n",
    "            dlog.setVisible(true);\n",
    "        }\n",
    "    }\n",
    "    \n",
    "    //# クラス内からしか使わないのでアクセス修飾子はprivate\n",
    "    private void prefDisplay () {\n",
    "        StringBuffer temp = new StringBuffer();\n",
    "        temp.append(\"1\" + \"\\t\");\n",
    "        temp.append(\"Hokkaido\" + \"\\n\");\n",
    "        temp.append(\"2\" + \"\\t\");\n",
    "        temp.append(\"Aomori\" + \"\\n\");\n",
    "        temp.append(\"3\" + \"\\t\");\n",
    "        temp.append(\"Iwate\" + \"\\n\");\n",
    "        temp.append(\"4\" + \"\\t\");\n",
    "        temp.append(\"Miyagi\" + \"\\n\");\n",
    "        temp.append(\"5\" + \"\\t\");\n",
    "        temp.append(\"Akita\" + \"\\n\");\n",
    "        txtar1.setText(temp.toString());\n",
    "    }\n",
    "            \n",
    "}\n",
    "\n",
    "//# ActinListernerを実装したオリジナルダイアログボックスクラスを定義\n",
    "class ExitDialog extends Dialog implements ActionListener {\n",
    "    public ExitDialog(Frame parent, String title){\n",
    "        //# スーパークラスのコンストラクタを呼び出す\n",
    "        super(parent, title);\n",
    "        \n",
    "        //# ダイアログボックスを閉じるとき\n",
    "        addWindowListener( new WindowAdapter(){\n",
    "            public void windowClosing(WindowEvent e) {\n",
    "                setVisible(false);\n",
    "            }\n",
    "        });\n",
    "        \n",
    "        //# ダイアログボックス設定\n",
    "        setLocation(320, 300);\n",
    "        setSize(220,80);\n",
    "        setLayout(new FlowLayout(FlowLayout.CENTER,10,10));\n",
    "        setBackground(Color.LIGHT_GRAY);\n",
    "        \n",
    "        //ボタンの設定\n",
    "        Button btnYes = new Button(\"はい\");\n",
    "        Button btnNo = new Button(\"いいえ\");\n",
    "        \n",
    "        Font f = new Font (\"Dialog\", Font.PLAIN, 14);\n",
    "        btnYes.setFont(f);\n",
    "        btnNo.setFont(f);\n",
    "        btnYes.setForeground(new Color(255,0,0));\n",
    "        \n",
    "        btnYes.addActionListener(this);\n",
    "        btnNo.addActionListener(this);\n",
    "        \n",
    "        add(btnYes);\n",
    "        add(btnNo);\n",
    "    }\n",
    "    \n",
    "    //# イベントハンドラactionPerfomed()メソッド\n",
    "    public void actionPerformed(ActionEvent e){\n",
    "        //# getActionCommandメソッドでクリックされたボタンのコマンド名を取得し、文字列として変数に格納している\n",
    "        //# ここはコマンド名が無いのでボタンに表示されている文字が取得される\n",
    "        String str = e.getActionCommand();\n",
    "        //# 文字列は参照型なのでequals()メソッドを使う。==は使えない\n",
    "        if( str.equals(\"はい\")){\n",
    "            System.exit(0);        \n",
    "        } else if( str.equals(\"いいえ\")){\n",
    "            setVisible(false);\n",
    "        }\n",
    "    }\n",
    "}\n",
    "\n",
    "public class $name {\n",
    "    public static void main(String args[]){\n",
    "        PrefFrame frm = new PrefFrame(\"都道府県マスター\");\n",
    "        //# フレームの設定\n",
    "        frm.setLocation(300,200);\n",
    "        frm.setSize(250,350);\n",
    "        frm.setBackground(Color.LIGHT_GRAY);\n",
    "        frm.setVisible(true);\n",
    "    }\n",
    "}\n",
    "EOS\n",
    "\n",
    "### コンパイル\n",
    "#javac ${name}.java\n",
    "javac -encoding UTF-8 ${name}.java\n",
    "\n",
    "### 実行\n",
    "#java -classpath ${classpath} ${name}\n",
    "java ${name}"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
